<?php

require_once APP_PATH . '/app/models/UserModel.php';
require_once APP_PATH . '/app/models/TWtf.php';

use Phalcon\Mvc\Controller;

class LoginController extends BaseController
{

    function indexAction()
    {
        $this->view->enable();
    }

    //登陆检查
    function loginAction()
    {
        $paramlist = array(
            'phone' => 'string',
            'password' => 'string'
        );
        $what = '';
        $params = common::check_json_keys($this->jsdata, $paramlist, $what);
        if (!$params) {
            $this->resp['status'] = -1;
            $this->resp['message'] = $what;
            return;
        }

        $userModel = new UserModel;
        $res = $userModel->check($params);
        if (!$res) {
            $this->resp['status'] = last_error_code();
            $this->resp['message'] = last_error_msg();
            return;
        }

        $this->session->set("user", $params['phone']);
        $this->resp = array(
            'status' => 0,
            'message' => 'OK',
        );

        $s = "find session";
        if (!$this->session->has("user")) {
            $s = "no session";
        }
        hilog::debug($s);
        $test = $this->session->get('user');
        hilog::debug("user=$test");

        /*$TWtf = new TWtf;
        $TWtf->uid       = 111111;  
        $TWtf->name = 'wangjinjiang';   
        //$TWtf->create_time  = TimeUtils::getFullTime();
        if (! $TWtf->save()) {  
           foreach ($TWtf->getMessages() as $message) {  
               echo "Message: ", $message->getMessage();  
               echo "Field: ", $message->getField();  
               echo "Type: ", $message->getType();  
           }  
       } else {
           
       }*/
    }

    //登出
    function logoutAction()
    {
        if ($this->session->has("user")) {
            $this->session->remove("user");
//            $this->session->destory();
        }
        $this->resp = array('status' => 0, 'message' => 'ok');
    }

}
